package org.megha.blog.example.part11;

import java.util.LinkedList;
import java.util.List;

import org.megha.blog.example.part6.Cylinder;
import org.megha.blog.example.part6.Sphere;
import org.megha.blog.example.part6.ThreeDObject;

public class LinkedListExample {

	public static void main(String args[]) {
		// create a list backed by a linked-list
		List<ThreeDObject> objects = new LinkedList<ThreeDObject>();

		// throw some 3d objects into the list
		objects.add(new Sphere(5));
		objects.add(new Sphere(88));
		objects.add(new Cylinder(7, 8));
		objects.add(new Cylinder(9.9, 5.5));

		// linked-list supports get(index), but
		// get() is an O(index) operation, so not recommended.
		for (int i = 0; i < objects.size(); i++) {
			System.out.println(objects.get(i));
		}

		// a better and faster way to traverse the list
		for (ThreeDObject obj : objects) {
			System.out.println(obj);
		}

		System.out.println("is list empty? " + objects.isEmpty()); // False
		ThreeDObject firstElement = objects.get(0);
		System.out.println("is the first element present? "
			+ objects.contains(firstElement)); // True

		System.out.println("deleting all elements in the list");
		objects.clear();

		System.out.println("is list empty? " + objects.isEmpty()); // True
		System.out.println("is the first element still present? "
			+ objects.contains(firstElement)); // False
	}
}
